﻿using LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    public interface IFieldUIElement
    {
        string FieldName { get; set; }
        string WidgetTypeName { get; set; }

        void SetFieldValue(string value, string defaultImagesPath = null);
        
        void RefreshErrorStatus(Brush errForeColor = null);

        Brush ForeColor { get; set; }
    }

    public class FieldTextBlock : TextBlock, IFieldUIElement
    {
        public string FieldName { get; set; }

        public string WidgetTypeName { get; set; }

        public void SetFieldValue(string text, string defaultImagesPath = null)
        {
            this.Text = text;
        }

        public Brush ForeColor { get; set; } = System.Windows.Media.Brushes.Black;

        public void RefreshErrorStatus(Brush errForeColor = null)
        {
            if (errForeColor == null)
            {
                this.Foreground = ForeColor;
            }
            else
            {
                this.Foreground = errForeColor;
            }
        }
    }

    public class FieldImage : Image, IFieldUIElement
    {
        public string FieldName { get; set; }

        public string WidgetTypeName { get; set; }

        public void SetFieldValue(string text, string defaultImagePath = null)
        {
            if (System.IO.File.Exists(text))
            {
                this.Source = Globals.LoadBitmapImageFromFile(text);
                return;
            }

            if (Directory.Exists(defaultImagePath) == false)
            {
                defaultImagePath = Globals.PathofWorkspaceImages;
            }
            try
            {
                string fullImagePath = Path.Combine(defaultImagePath, text.TrimStart(new char[] { '\\', '/' }));

                if (System.IO.File.Exists(fullImagePath))
                {
                    this.Source = Globals.LoadBitmapImageFromFile(fullImagePath);
                }
                else
                {
                    this.Source = MiniReportEditor.NoImageSource;
                }
            }
            catch
            {
                this.Source = MiniReportEditor.NoImageSource;
            }
        }
        public Brush ForeColor { get; set; }

        public void RefreshErrorStatus(Brush errForeColor = null)
        {
            // 空方法，无须实现
        }
    }

    public class FieldQrCoder : Image, IFieldUIElement
    {
        public string FieldName { get; set; }

        public string WidgetTypeName { get; set; }

        public void SetFieldValue(string text, string defaultImagesPath = null)
        {
            this.Source = MiniReportEditor.BuildCodeImage(text, this.ActualWidth, this.ActualHeight);
        }

        public Brush ForeColor { get; set; }
        public void RefreshErrorStatus(Brush errForeColor = null)
        {
            // 空方法，无须实现
        }
    }
}
